/*   1:    */ package cn.com.dashihui.pay.ZY;
/*   2:    */ 
/*   3:    */ import java.math.BigInteger;
/*   4:    */ import java.security.Key;
/*   5:    */ import java.security.KeyFactory;
/*   6:    */ import java.security.KeyPair;
/*   7:    */ import java.security.KeyPairGenerator;
/*   8:    */ import java.security.PrivateKey;
/*   9:    */ import java.security.PublicKey;
/*  10:    */ import java.security.SecureRandom;
/*  11:    */ import java.security.Signature;
/*  12:    */ import java.security.interfaces.RSAPublicKey;
/*  13:    */ import java.security.spec.PKCS8EncodedKeySpec;
/*  14:    */ import java.security.spec.X509EncodedKeySpec;
/*  15:    */ import java.util.HashMap;
/*  16:    */ import java.util.Map;
/*  17:    */ import javax.crypto.Cipher;

import org.rsa.sdk.utils.Base64;
/*  18:    */ 
/*  19:    */ public class RSA
/*  20:    */ {
/*  21: 45 */   private static int KEYSIZE = 1024;
/*  22:    */   public static final String CHAR_ENCODING = "UTF-8";
/*  23:    */   public static final String RSA_ALGORITHM = "RSA/ECB/PKCS1Padding";
/*  24:    */   
/*  25:    */   public static Map<String, String> generateKeyPair()
/*  26:    */     throws Exception
/*  27:    */   {
/*  28: 55 */     SecureRandom sr = new SecureRandom();
/*  29:    */     
/*  30: 57 */     KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
/*  31:    */     
/*  32: 59 */     kpg.initialize(KEYSIZE, sr);
/*  33:    */     
/*  34: 61 */     KeyPair kp = kpg.generateKeyPair();
/*  35:    */     
/*  36: 63 */     Key publicKey = kp.getPublic();
/*  37: 64 */     byte[] publicKeyBytes = publicKey.getEncoded();
/*  38: 65 */     String pub = new String(Base64.encodeBase64(publicKeyBytes), "UTF-8");
/*  39:    */     
/*  40: 67 */     Key privateKey = kp.getPrivate();
/*  41: 68 */     byte[] privateKeyBytes = privateKey.getEncoded();
/*  42: 69 */     String pri = new String(Base64.encodeBase64(privateKeyBytes), "UTF-8");
/*  43:    */     
/*  44: 71 */     Map<String, String> map = new HashMap();
/*  45: 72 */     map.put("publicKey", pub);
/*  46: 73 */     map.put("privateKey", pri);
/*  47: 74 */     RSAPublicKey rsp = (RSAPublicKey)kp.getPublic();
/*  48: 75 */     BigInteger bint = rsp.getModulus();
/*  49: 76 */     byte[] b = bint.toByteArray();
/*  50: 77 */     byte[] deBase64Value = Base64.encodeBase64(b);
/*  51: 78 */     String retValue = new String(deBase64Value);
/*  52: 79 */     map.put("modulus", retValue);
/*  53: 80 */     return map;
/*  54:    */   }
/*  55:    */   
/*  56:    */   public static String encrypt(String source, String publicKey)
/*  57:    */     throws Exception
/*  58:    */   {
/*  59: 87 */     Key key = getPublicKey(publicKey);
/*  60:    */     
/*  61: 89 */     Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
/*  62: 90 */     cipher.init(1, key);
/*  63: 91 */     byte[] b = source.getBytes();
/*  64:    */     
/*  65: 93 */     byte[] b1 = cipher.doFinal(b);
/*  66: 94 */     return new String(Base64.encodeBase64(b1), "UTF-8");
/*  67:    */   }
/*  68:    */   
/*  69:    */   public static String decrypt(String cryptograph, String privateKey)
/*  70:    */     throws Exception
/*  71:    */   {
/*  72:101 */     Key key = getPrivateKey(privateKey);
/*  73:    */     
/*  74:103 */     Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
/*  75:104 */     cipher.init(2, key);
/*  76:105 */     byte[] b1 = Base64.decodeBase64(cryptograph.getBytes());
/*  77:    */     
/*  78:107 */     byte[] b = cipher.doFinal(b1);
/*  79:108 */     return new String(b);
/*  80:    */   }
/*  81:    */   
/*  82:    */   public static PublicKey getPublicKey(String key)
/*  83:    */     throws Exception
/*  84:    */   {
/*  85:119 */     X509EncodedKeySpec keySpec = new X509EncodedKeySpec(Base64.decodeBase64(key.getBytes()));
/*  86:120 */     KeyFactory keyFactory = KeyFactory.getInstance("RSA");
/*  87:121 */     PublicKey publicKey = keyFactory.generatePublic(keySpec);
/*  88:122 */     return publicKey;
/*  89:    */   }
/*  90:    */   
/*  91:    */   public static PrivateKey getPrivateKey(String key)
/*  92:    */     throws Exception
/*  93:    */   {
/*  94:133 */     PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(Base64.decodeBase64(key.getBytes()));
/*  95:134 */     KeyFactory keyFactory = KeyFactory.getInstance("RSA");
/*  96:135 */     PrivateKey privateKey = keyFactory.generatePrivate(keySpec);
/*  97:136 */     return privateKey;
/*  98:    */   }
/*  99:    */   
/* 100:    */   public static String sign(String content, String privateKey)
/* 101:    */   {
/* 102:140 */     String charset = "UTF-8";
/* 103:    */     try
/* 104:    */     {
/* 105:142 */       PKCS8EncodedKeySpec priPKCS8 = new PKCS8EncodedKeySpec(Base64.decodeBase64(privateKey.getBytes()));
/* 106:143 */       KeyFactory keyf = KeyFactory.getInstance("RSA");
/* 107:144 */       PrivateKey priKey = keyf.generatePrivate(priPKCS8);
/* 108:    */       
/* 109:146 */       Signature signature = Signature.getInstance("SHA1WithRSA");
/* 110:    */       
/* 111:148 */       signature.initSign(priKey);
/* 112:149 */       signature.update(content.getBytes(charset));
/* 113:    */       
/* 114:151 */       byte[] signed = signature.sign();
/* 115:    */       
/* 116:153 */       return new String(Base64.encodeBase64(signed));
/* 117:    */     }
/* 118:    */     catch (Exception localException) {}
/* 119:158 */     return null;
/* 120:    */   }
/* 121:    */   
/* 122:    */   public static boolean checkSign(String content, String sign, String publicKey)
/* 123:    */   {
/* 124:    */     try
/* 125:    */     {
/* 126:163 */       KeyFactory keyFactory = KeyFactory.getInstance("RSA");
/* 127:164 */       byte[] encodedKey = Base64.decode2(publicKey);
/* 128:165 */       PublicKey pubKey = keyFactory.generatePublic(new X509EncodedKeySpec(encodedKey));
/* 129:    */       
/* 130:167 */       Signature signature = Signature.getInstance("SHA1WithRSA");
/* 131:    */       
/* 132:169 */       signature.initVerify(pubKey);
/* 133:170 */       signature.update(content.getBytes("utf-8"));
/* 134:    */       
/* 135:172 */       return signature.verify(Base64.decode2(sign));
/* 136:    */     }
/* 137:    */     catch (Exception e)
/* 138:    */     {
/* 139:176 */       e.printStackTrace();
/* 140:    */     }
/* 141:179 */     return false;
/* 142:    */   }
/* 143:    */ }


/* Location:           D:\公司项目\K00一账通\10031收银台\10031-payservice-v1.2.93.1.02.jar
 * Qualified Name:     com.ylsoft.pcore.business.service.sign.RSA
 * JD-Core Version:    0.7.0.1
 */